<?php

App::uses('AppController', 'Controller');

/**
 * traumas Controller
 *
 * @property Trauma $Trauma
 */
class TraumasController extends AppController {

    public $layout = 'hospital';
    public $uses = array('Trauma', 'Concepto', 'Alerta','Scor','Parametroscor');

    /**
     * index method
     *
     * @return void
     * 
     */
    public function beforeFilter() {
        parent::beforeFilter();
        //$this->Auth->allow();
    }

    public function index() {
        //$this->Paciente->recursive = 0;
        //$this->set('pacientes', $this->paginate());
        $traumas = $this->Trauma->find('all', array('recursive' => -1));
        $this->set(compact('traumas'));
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->Trauma->exists($id)) {
            throw new NotFoundException(__('Invalid trauma'));
        }
        $options = array('conditions' => array('Trauma.' . $this->Trauma->primaryKey => $id));
        $this->set('trauma', $this->Trauma->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {

        if ($this->request->is('post')) {
            //debug($this->request->data); exit;
            $this->Trauma->create();
            if ($this->Trauma->save($this->request->data)) {
                $this->Session->setFlash(__('The trauma has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The trauma could not be saved. Please, try again.'));
            }
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        $this->Trauma->id = $id;
        if (!$this->Trauma->exists($id)) {
            throw new NotFoundException(__('Invalid paciente'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            //debug($this->request->data);exit;

            if ($this->Trauma->save($this->request->data)) {
                $this->Session->setFlash(__('The trauma has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The trauma could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Trauma.' . $this->Trauma->primaryKey => $id));
            $this->request->data = $this->Trauma->find('first', $options);
        }
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @throws MethodNotAllowedException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Trauma->id = $id;

        if (!$this->Trauma->exists()) {
            throw new NotFoundException(__('Invalid trauma'));
        }
        // $this->request->onlyAllow('post', 'delete');

        if ($this->Trauma->delete()) {
            $this->Session->setFlash(__('Trauma deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Trauma was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

    public function sintomas($id = null) {
        $dato = $this->Trauma->findById($id);
        $nombre = $dato['Trauma']['nombre'];
        $sintomas = $this->Concepto->find('all', array(
            'conditions' => array('Concepto.trauma_id' => $id)
        ));
        
        $alertas = $this->Alerta->find('all', array('recursive' => -1));
        $scors= $this->Scor->find('all', array('recursive'=>-1));
        $this->set(compact('sintomas', 'nombre', 'alertas', 'id','scors'));
        if (!empty($this->request->data)) {
            //debug($this->request->data);exit;
            $id = $this->request->data['Concepto']['trauma_id'];

            if ($this->Concepto->save($this->request->data)) {
                $this->Session->setFlash('CORRECTO: Guardado correctamente');
                $this->redirect(array('action' => 'sintomas', $id));
            } else {
                $this->Session->setFlash('ERROR: No guardo!!');
                $this->redirect(array('action' => 'sintomas', $id));
            }
        }
    }

    public function ajaxcargaedit($id = null) {
        $this->layout='ajax';
        
        $this->Concepto->id = $id;
        
        if (!empty($this->request->data)) {
            //debug($this->request->data);exit;
            if ($this->Concepto->save($this->request->data)) {
                $idTrauma= $this->request->data['Concepto']['trauma_id'];
                $this->Session->setFlash('CORRECTO: Se cambiaron los datos correctamente');
                $this->redirect(array('action' => 'sintomas', $idTrauma));
            } else {
                $this->Session->setFlash('ERROR: No guardo!!');
                $this->redirect(array('action' => 'sintomas', $idTrauma));
            }
        } else {
            $options = array('conditions' => array('Concepto.' . $this->Concepto->primaryKey => $id));
            $this->request->data = $this->Concepto->find('first', $options);
            $alertas = $this->Alerta->find('all', array('recursive' => -1));
            $id = $this->request->data['Concepto']['trauma_id'];
            $scors= $this->Scor->find('all', array('recursive'=>-1));
            //debug($scors);exit;
            $this->set(compact('alertas', 'id','scors'));
        }
    }

    public function deletesintoma($id=null , $idTrauma=null) {
        if ($this->Concepto->delete($id)) {
            $this->Session->setFlash('CORRECTO: Se cambiaron los datos correctamente');
            $this->redirect(array('action' => 'sintomas', $idTrauma));
        } else {
            $this->Session->setFlash('ERROR: No guardo!!');
            $this->redirect(array('action' => 'sintomas', $idTrauma));
        }
    }
    public function ajaxcargaver($id=null){
        $this->layout='ajax';
        $sintoma = $this->Concepto->find('first', array(
            'conditions'=>array('Concepto.id'=>$id)
        ));
        $idScor= $sintoma['Concepto']['scor_id'];
        $alertas = $this->Parametroscor->find('all', array('Parametroscor.scor_id'=>$idScor));
        debug($sintoma);exit;
        $this->set(compact('sintoma'));
    }
}
